﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;

namespace RMS.API.Web.Areas.Demo.Controllers
{
    /// <summary>
    /// 接口样例
    /// </summary>
    [RoutePrefix("api/Demo/Default")]
    public class ValuesController : ApiController
    {
        /// <summary>
        /// 无参Get请求，返回数组
        /// </summary>
        /// <returns></returns>
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
        /// <summary>
        /// 有参Get请求，返回字符串
        /// </summary>
        /// <param name="id">int参数</param>
        /// <param name="dt">DateTime参数</param>
        /// <returns></returns>
        public object Get(int id,DateTime dt)
        {
            return new { id = id, dt = dt };
        }
        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="value">表单数据</param>
        public string Post([FromBody]string value)
        {
            return value;
        }
        /// <summary>
        /// Put请求
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        public object Put(int id, [FromBody]string value)
        {
            return new { id = id, value = value };
        }
        /// <summary>
        /// Delete请求
        /// </summary>
        /// <param name="id"></param>
        public int Delete(int id)
        {
            return id;
        }
        /// <summary>
        /// 异常处理测试
        /// </summary>
        /// <returns></returns>
        [Route("ErrorTest")]
        public string ErrorTest()
        {
            throw new Exception("发生异常");
        }

        /// <summary>
        /// 自定义名称方法
        /// </summary>
        /// <remarks>
        /// 这里可以写备注
        /// 
        /// 各种备注
        /// 
        ///     POST /Todo
        ///     {
        ///        "key": "0e7ad584-7788-4ab1-95a6-ca0a5b444cbb",
        ///        "name": "Item1",
        ///        "isComplete": true
        ///     }
        /// 
        /// </remarks>
        /// <param name="id">未使用FromBody修饰的参数，可以Get提交</param>
        /// <param name="value">Form表单参数，特性修饰的参数必须唯一</param>
        /// <returns></returns>
        /// <response code="200">执行成功</response>
        /// <response code="500">接口错误</response>
        [HttpPost]
        [Route("MyFunction")]
        public TestModel MyFunction(int id ,[FromBody]TestModel value)
        {
            return value;
        }

        /// <summary>
        /// 上传测试
        /// </summary>
        /// <param name="f">上传提示参数</param>
        /// <returns></returns>
        [HttpPost]
        [Route("Upload")]
        public string Upload([SwaggerFileUpload] string f)
        {
            string result = "";
            HttpFileCollection filelist = HttpContext.Current.Request.Files;
            if (filelist != null && filelist.Count > 0)
            {
                for (int i = 0; i < filelist.Count; i++)
                {
                    HttpPostedFile file = filelist[i];
                    result += file.FileName;
                }
            }
            else
            {
                result = "上传的文件信息不存在！";
            }
            return result;
        }

        /// <summary>
        /// 测试模型
        /// </summary>
        public class TestModel
        {
            /// <summary>
            /// 模型
            /// </summary>
            [Required]
            public string Name { get; set; }
            /// <summary>
            /// 年龄
            /// </summary>
            public int Age { get; set; }
            /// <summary>
            /// 删除状态
            /// </summary>
            [DefaultValue(false)]
            public bool IsDeleted { get; set; }
            /// <summary>
            /// 生日
            /// </summary>
            public DateTime Birthday { get; set; }
        }
    }
}